Import('env', 'cereal', 'cython_dependencies')

import os
from opendbc.can.process_dbc import process

dbcs = []
for x in sorted(os.listdir('../')):
  if x.endswith(".dbc"):
    def compile_dbc(target, source, env):
      process(source[0].path, target[0].path)
    in_fn = [os.path.join('../', x), 'dbc_template.cc']
    out_fn = os.path.join('dbc_out', x.replace(".dbc", ".cc"))
    dbc = env.Command(out_fn, in_fn, compile_dbc)
    dbcs.append(dbc)


libdbc = env.SharedLibrary('libdbc', ["dbc.cc", "parser.cc", "packer.cc", "common.cc"]+dbcs, LIBS=["capnp", "kj"])

# Build packer and parser
env.Command(['packer_pyx.so', 'packer_pyx.cpp', 'parser_pyx.so', 'parser_pyx.cpp'],
            cython_dependencies + [libdbc, cereal, 'common_pyx_setup.py', 'common.pxd', 'packer_pyx.pyx', 'parser_pyx.pyx', 'packer.cc', 'parser.cc'],
            "cd opendbc/can && python3 common_pyx_setup.py build_ext --inplace")
